﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;

namespace Borton.Data
{
    /// <summary>
    /// Summary description for DAL.
    /// </summary>
    public static class ConnectionAdapter
    {
        //private static string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\..\\borton.mdb";
        //private static string connectionString = "Provider=SQLOLEDB;Data Source=Your_Server_Name;Initial Catalog= Your_Database_Name;UserId=Your_Username;Password=Your_Password;";
        private static string connectionString = "Provider=SQLOLEDB;Data Source=PPA\\SQLEXPRESS;Initial Catalog=borton;Integrated Security=SSPI;";

        private static OleDbConnection connection = new OleDbConnection(connectionString);

        private static long inUse = 0;

        public static OleDbConnection getOpenConnection()
        {
            inUse++;
            if (!connection.State.Equals(ConnectionState.Open))
            {
                try
                {
                    connection.Open();
                } catch (Exception e) {
                    Console.WriteLine(e.Message);
                    throw e;
                }
            }
            return connection;
        }

        public static void closeConnection()
        {
            inUse--;
            if (inUse == 0)
            {
                connection.Close();
            }
        }

    }
}




